package com.ly.emp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.emp.bean.entity.HubTeamIndexType;
import com.ly.emp.bean.vo.HubTeamIndexTypeVO;
import com.ly.emp.constant.EmpErrorConstants;
import com.ly.emp.mapper.HubTeamIndexTypeMapper;
import com.ly.emp.service.IHubTeamIndexTypeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 一线班组-指标库类型表-impl
 *
 * @author luoyun
 * @Date: 2025-06-20
 */
@Service
public class HubTeamIndexTypeServiceImpl extends ServiceImpl<HubTeamIndexTypeMapper, HubTeamIndexType> implements IHubTeamIndexTypeService {

    @Override
    public R getId(String id) {
        HubTeamIndexType entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamIndexType entity) {
        String orgCode = entity.getOrgCode();
        if(StrUtil.isBlank(orgCode)){
            return R.fail(EmpErrorConstants.NO_TEAM);
        }
        String indexTypeName = entity.getIndexTypeName();
        List<HubTeamIndexType> list = this.list(
                new LambdaQueryWrapper<HubTeamIndexType>()
                        .eq(StrUtil.isNotBlank(entity.getOrgCode()),HubTeamIndexType::getOrgCode, entity.getOrgCode())
                        .like(StrUtil.isNotBlank(entity.getIndexTypeName()),HubTeamIndexType::getIndexTypeName,entity.getIndexTypeName())
                        .orderByAsc(HubTeamIndexType::getDisplayOrder)
                        .orderByDesc(HubTeamIndexType::getSysUpdateDate)
        );
        //搜索时，不再封装成上下级
        if(StrUtil.isNotBlank(indexTypeName)){
            return R.ok(list);
        }
        List<HubTeamIndexTypeVO> vos = wapperChildren("0", list);
        return R.ok(vos);
    }

    /**
     * 封装上下级
     * @param parentCode
     * @param list
     * @return
     */
    private List<HubTeamIndexTypeVO> wapperChildren(String parentCode, List<HubTeamIndexType> list){
        List<HubTeamIndexTypeVO> vos = new ArrayList<>();
        if(CollUtil.isEmpty(list)){
            return null;
        }
        list.stream().filter(n -> n.getParentTypeCode().equals(parentCode)).forEach(n -> {
            // System.out.println("当前遍历的数据为："+n.getIndexTypeName());
            HubTeamIndexTypeVO vo = BeanUtil.copyProperties(n,HubTeamIndexTypeVO.class);
            List<HubTeamIndexTypeVO> childrenList = wapperChildren(n.getIndexTypeCode(),list);
            vo.setChildren(CollUtil.isEmpty(childrenList) ? null : childrenList);
            vos.add(vo);
        });
        return vos;
    }

    @Override
    public R getPage(HubTeamIndexType entity, Page<HubTeamIndexType> page) {
        IPage<HubTeamIndexType> pageList = this.page(
                page,
                new LambdaQueryWrapper<HubTeamIndexType>()
                        .orderByDesc(HubTeamIndexType::getDisplayOrder)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamIndexType entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamIndexType entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamIndexType> dto) {
        List<HubTeamIndexType> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public String getAndAdd(HubTeamIndexType entity) {
        HubTeamIndexType one = this.getOne(
                new LambdaQueryWrapper<HubTeamIndexType>()
                        .eq(HubTeamIndexType::getOrgCode, entity.getOrgCode())
                        .eq(HubTeamIndexType::getIndexTypeName, entity.getIndexTypeName())
        );
        if(one != null){
            return one.getIndexTypeCode();
        }
        //设置默认父编码
        if (StrUtil.isBlank(entity.getParentTypeCode())) {
            entity.setParentTypeCode("0");
        }
        String uidCode = IdUtil.getSnowflakeNextIdStr();
        entity.setIndexTypeCode(uidCode);
        this.save(entity);
        return uidCode;
    }
}
